package com.stone.customer.console.api.controller.v1;

import com.stone.customer.core.domain.entity.Complaints;
import com.stone.customer.core.domain.repository.ComplaintsRepository;
import com.stone.customer.core.infra.mapper.ComplaintsMapper;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 用户投诉建议表 管理 API
 *
 * @author Mr_wenpan@163.com 2021-07-19 21:54:57
 */
@RestController("complaintsController.v1")
@RequestMapping("/v1/{organizationId}/complaintss")
public class ComplaintsController extends BaseController {

    @Autowired
    private ComplaintsRepository complaintsRepository;
    @Autowired
    private ComplaintsMapper complaintsMapper;

    @ApiOperation(value = "用户投诉建议表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<List<Complaints>> list(Complaints complaints, @ApiIgnore @SortDefault(value = Complaints.FIELD_COMPLAINTS_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        // Page<Complaints> list = complaintsRepository.pageAndSort(pageRequest, complaints);
        List<Complaints> complaintsList = complaintsMapper.selectTest();
        return Results.success(complaintsList);
    }

    @ApiOperation(value = "用户投诉建议表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{complaintsId}")
    public ResponseEntity<Complaints> detail(@PathVariable Long complaintsId) {
        Complaints complaints = complaintsRepository.selectByPrimaryKey(complaintsId);
        return Results.success(complaints);
    }

    @ApiOperation(value = "创建用户投诉建议表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<Complaints> create(@RequestBody Complaints complaints) {
        validObject(complaints);
        complaintsRepository.insertSelective(complaints);
        return Results.success(complaints);
    }

    @ApiOperation(value = "修改用户投诉建议表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<Complaints> update(@RequestBody Complaints complaints) {
        SecurityTokenHelper.validToken(complaints);
        complaintsRepository.updateByPrimaryKeySelective(complaints);
        return Results.success(complaints);
    }

    @ApiOperation(value = "删除用户投诉建议表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody Complaints complaints) {
        SecurityTokenHelper.validToken(complaints);
        complaintsRepository.deleteByPrimaryKey(complaints);
        return Results.success();
    }

    @ApiOperation(value = "线程池测试")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/thread-pool-test")
    public ResponseEntity<List<Complaints>> threadPoolQuestionTest() {
        complaintsRepository.threadPoolQuestionTest();
        return Results.success();
    }

}
